/**
 * @author 黄波
 * 创建日期：2011Sep 14, 2011
 * TODO// N/A
 */
package com.platform.user.action;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.platform.constant.ResponseCode;
import com.platform.os.util.Util;
import com.platform.pojo.UserInfo;
import com.platform.user.bo.UserBo;

/**
 * 更新用户信息
 */
public class UpdateUserAction extends UserSupport {

	private String id;
	private UserInfo user;
	private boolean success;
	private String msg;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public UserInfo getUser() {
		return user;
	}
	public void setUser(UserInfo user) {
		this.user = user;
	}
	public boolean isSuccess() {
		return success;
	}
	public void setSuccess(boolean success) {
		this.success = success;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	@Override
	public String execute() throws Exception {
		if(user == null) {
			msg = ResponseCode.FAILURE_UPDATE;
		} else {
			UserBo bo = UserBo.getInstance();
			UserInfo dbUser = bo.getUser(user.getId().toString());
			dbUser.setRealName(user.getRealName());
			dbUser.setEmail(user.getEmail());
			dbUser.setMobile(user.getMobile());
			dbUser.setScore(user.getScore());
			dbUser.setGroupId(user.getGroupId());
			dbUser.setDepartmentId(user.getDepartmentId());
			dbUser.setProfessionalId(user.getProfessionalId());
			dbUser.setGradeId(user.getGradeId());
			dbUser.setClassId(user.getClassId());
//			user.setId(Integer.parseInt(id));
//			user.setLastLoginIp(Util.getReq().getRemoteAddr());
//			user.setLastLoginTime(new Date());
			if(bo.updateUser(dbUser)) {
				msg = ResponseCode.SUCCESS_UPDATE;
			} else {
				msg = ResponseCode.FAILURE_DB_ERROR;
			}
		}
		success = true;
//		HashMap map = new HashMap();
//		map.put("msg", msg);
//		map.put("success", success);
//		JSONObject json = JSONObject.fromObject(map);
//		HttpServletResponse response = ServletActionContext.getResponse();
//		response.setCharacterEncoding("utf-8");
//		PrintWriter out = response.getWriter();
//		out.println(json);
//		out.flush();
//		out.close();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.flush();
		out.write("<script>alert('" + msg + "');history.back();</script>");
		out.close();
		return null;
	}
	
}
